﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace EmailClient.DAO
{
    class ConnectDataBase
    {
        static string strConnection = "Data Source=(local);Initial Catalog=EmailClient;Integrated Security=True;Pooling=False";
        
        static SqlConnection conn;
        static SqlCommand cmd;
        static SqlDataAdapter da;
        static DataTable dt;
        public static void OpenConnection()
        {
            conn = new SqlConnection(strConnection);
            conn.Open();
        }
        public static void CloseConnection()
        {
            conn.Close();
        }
        public static void ExcuNonQuery(string sql)
        {
            OpenConnection();
            cmd = conn.CreateCommand();
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
            CloseConnection();
        }
        public static DataTable ExcuQuery(string sql)
        {
            DataTable dt = new DataTable();
            try
            {
                OpenConnection();
                cmd = conn.CreateCommand();
                cmd.CommandText = sql;
                da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(dt);
                CloseConnection();
            }
            catch
            {
                CloseConnection();
            }
            return dt;
        }
    }
}
